
package com.hyst.satelite;

/**
 * @Description:
 * @Date：2018年11月28日 @author： DongY
 * @version ：1.0 1.[2018年11月28日]CreatBy：dy
 */

public class ByteUtils {

	public static String toBitStr(final byte by) {
		final StringBuffer sb = new StringBuffer();
		sb.append((by >> 7) & 0x1).append((by >> 6) & 0x1).append((by >> 5) & 0x1);
		sb.append((by >> 4) & 0x1);
		sb.append((by >> 3) & 0x1);
		sb.append((by >> 2) & 0x1);
		sb.append((by >> 1) & 0x1);
		sb.append((by >> 0) & 0x1);
		return sb.toString();
	}

	public static String toBitStr(final byte[] bts) {
		final StringBuffer str = new StringBuffer();
		for (final byte b : bts) {
			str.append(toBitStr(b));
		}
		return str.toString();
	}

	public static int toInt(final byte[] bts) {
		final String str = toBitStr(bts);

		return Integer.valueOf(str, 2);
	}

	public static int toInt(final byte[] bts, final int strart) {
		String str = toBitStr(bts);
		str = str.substring(strart);
		return Integer.valueOf(str, 2);
	}

	public static int toInt(final byte[] bts, final int strart, final int end) {
		String str = toBitStr(bts);
		str = str.substring(strart, end);
		return Integer.valueOf(str, 2);
	}

}
